package com.yang.Filter;
import com.yang.constant.ConfiConstant;
import org.jboss.logging.Logger;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


public class AdminFilter implements Filter {
    private static Logger logger = Logger.getLogger(AdminFilter.class);
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse)servletResponse;

        String message = (String) request.getSession().getAttribute("message");
        String identification = (String) request.getSession().getAttribute("identfication");
        logger.info(message);
        logger.info("identification ="+identification);
       if(message != null && message.equals("succeed Login") && identification != null && identification.equals(ConfiConstant.ADMINISTRATOR_USER)){
            logger.info("登陆成功");
            chain.doFilter(servletRequest,servletResponse);
        }else{
            logger.error("未登陆！");
            response.sendRedirect(request.getContextPath()+"/login.html");
        }

    }

    @Override
    public void destroy() {

    }
}
